﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Windows;

namespace personalplaner.common.utilities
{
	public class SystemIconHelper
	{
		private static Dictionary<string, BitmapSource> _systemIcons;
		public static Dictionary<string, BitmapSource> SystemIcons
		{
			get
			{
				if (_systemIcons == null)
				{
					//_systemIcons = new Dictionary<string, BitmapSource>();
					ExtractAssociatedIconEx(out _systemIcons);
				}

				return _systemIcons;
			}
		}

		private static void ExtractAssociatedIconEx(out Dictionary<string, BitmapSource> icons)
		{
			icons = new Dictionary<string, BitmapSource>();

			// Get the c:\ directory.
			System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\");

			// For each file in the c:\ directory, create a ListViewItem
			// and set the icon to the icon extracted from the file.
			foreach (System.IO.FileInfo file in dir.GetFiles())
			{
				// Set a default icon for the file.
				Icon iconForFile = System.Drawing.SystemIcons.WinLogo;

				//item = new ListViewItem(file.Name, 1);
				iconForFile = Icon.ExtractAssociatedIcon(file.FullName);

				// Check to see if the image collection contains an image
				// for this extension, using the extension as a key.
				if (!icons.ContainsKey(file.Extension))
				{
					// If not, add the image to the image list.
					iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName);

					BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(iconForFile.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
					icons.Add(file.FullName, bs);

				}
			}
		}

		public static BitmapSource ExtractAssociatedIcon(string name)
		{
			// Get the c:\ directory.
			//System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(name);

			//// For each file in the c:\ directory, create a ListViewItem
			//// and set the icon to the icon extracted from the file.
			//foreach (System.IO.FileInfo file in dir.GetFiles())
			//{
			//    // Set a default icon for the file.
			Icon iconForFile = System.Drawing.SystemIcons.WinLogo;

			//item = new ListViewItem(file.Name, 1);
			iconForFile = Icon.ExtractAssociatedIcon(name);

			BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(iconForFile.Handle, Int32Rect.Empty,
			                                                                             BitmapSizeOptions.FromEmptyOptions());

			return bs;
		}

		//public static void PrinterInfo(string printername)
		//{
		//    string printerName = "YourPrinterName";
		//    string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
		//    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
		//    ManagementObjectCollection coll = searcher.Get();

		//    foreach (ManagementObject printer in coll)
		//    {
		//        foreach (PropertyData property in printer.Properties)
		//        {
		//            Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
		//        }
		//    }
		//}
	}
}
